package com.atguigu.tingshu.user.startagy.impl;

import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.startagy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component("1002")
@Slf4j
public class TrackStrategy implements ItemTypeStrategy {
    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private AlbumFeignClient albumFeignClient;


    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("声音策略模式执行。。。");
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        Long userId = userPaidRecordVo.getUserId();
        String orderNo = userPaidRecordVo.getOrderNo();
        //声音
        if(!CollectionUtils.isEmpty(itemIdList)){
            Long count = userPaidTrackMapper.selectCount(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getOrderNo, orderNo));
            if(count > 0){
                return;
            }
            Result<TrackInfo> trackInfoById = albumFeignClient.getTrackInfoById(itemIdList.get(0));
            TrackInfo trackInfo = trackInfoById.getData();
            Long albumId = trackInfo.getAlbumId();
            itemIdList.stream().forEach(item->{
                UserPaidTrack userPaidTrack = new UserPaidTrack();
                userPaidTrack.setOrderNo(orderNo);
                userPaidTrack.setUserId(userId);
                userPaidTrack.setAlbumId(albumId);
                userPaidTrack.setTrackId(item);
                userPaidTrackMapper.insert(userPaidTrack);
            });
        }
    }
}
